<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 权限接口
 *
 * @author      $Author$
 * @package     admin
 * @subpackage  Controller
 */

class Permissions extends CT_Controller
{
    public function __construct()
    {
    	parent::__construct();
    }


    /**
     * 判断权限名称是否已经存在
     */

    public  function check_permissions_name()
    {
        $this->load->model('permissions_model');
        $name=$this->input->get("permissions_name",TRUE);
        $permissions_id=$this->input->get("permissions_id",TRUE)?$this->input->get("permissions_id",TRUE):NULL;
        if(!empty($name))
        {
            $res=$this->permissions_model->check_permissions_name($name,$permissions_id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }


    /**
     * 判断导航名称是否已经存在
     */

    public  function check_navigation_name()
    {
        $this->load->model('navigation_model');
        $name=$this->input->get("navigation_name",TRUE);
        $navigation_id=$this->input->get("navigation_id",TRUE)?$this->input->get("navigation_id",TRUE):NULL;
        if(!empty($name))
        {
            $res=$this->navigation_model->check_navigation_name($name,$navigation_id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }


    /**
     * 验证角色名称是否重复
     */

    public  function check_role_name()
    {
        $this->load->model('role_model');
        $name=$this->input->get("role_name",TRUE);
        $role_id=$this->input->get("role_id",TRUE)?$this->input->get("role_id",TRUE):NULL;
        if(!empty($name))
        {
            $res=$this->role_model->check_role_name($name,$role_id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }


    /**
     * 验证管理员名称是否重复
     */

    public  function  check_admin_name()
    {
        $this->load->model('admin_model');
        $admin_name=$this->input->get("admin_name",TRUE);
        $admin_id=$this->input->get("admin_id",TRUE)?$this->input->get("admin_id",TRUE):NULL;
        if(!empty($admin_name))
        {
            $res=$this->admin_model->check_admin_name($admin_name,$admin_id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }

    }

    /**
     * 验证添加管理员时密码是否最小为6位
     */

    public  function  check_admin_pass()
    {
        $admin_pass=$this->input->get("admin_pass",TRUE);

        if(!empty($admin_pass))
        {

            if(strlen($admin_pass)<6){
                $return =array('state' => 'success','error_type'=>1);
            }else if(strlen($admin_pass)>30){
                $return =array('state' => 'success','error_type'=>2);
            }else{
                $return =array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }


    /**
     * 验证名称字段是否含有特殊字符
     */
    public  function has_special_character()
    {
        $name=$this->input->get("name",TRUE);
        $pattern="/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_\-]+$/u";
        if(preg_match($pattern,$name)){
          $return=array('state' => 'fail');
        }else{
            $return =array('state' => 'success');
        }

        echo json_encode($return);
    }

    /**
     * 判断权限链接是否已经存在
     */

    public function  check_permissions_link()
    {
        $this->load->model('permissions_model');
        $link=$this->input->get("link",TRUE);
        $id=$this->input->get("id",TRUE)?$this->input->get("id",TRUE):NULL;
        if(!empty($link))
        {
            $res=$this->permissions_model->check_link($link,$id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }

    /**
     * 判断导航链接是否已经存在
     */

    public function  check_navigation_link()
    {
        $this->load->model('navigation_model');
        $link=$this->input->get("link",TRUE);
        $id=$this->input->get("id",TRUE)?$this->input->get("id",TRUE):NULL;
        if(!empty($link))
        {
            $res=$this->navigation_model->check_link($link,$id);
            if(!empty($res))
            {
                $return = array('state' => 'success');
            }else{
                $return = array('state' => 'fail');
            }

            echo json_encode($return);

        }
    }


}